#pragma once

#include <compl.h>



#if defined(__linux__) && !defined(DONOT_USE_RSEQ)
	#include <unix/ddblk/linux_rseq.h>
	#define COREID_BY_RSEQ
	SAI cid_t mp_coreid(){
		return rseqext_get_cpuhint(get_rseqext()); 
	}
#else
	#if defined(__APPLE__)
		#ifdef __amd64__
		#include <cpuid.h>

		#define CPUID(INFO, LEAF, SUBLEAF) __cpuid_count(LEAF, SUBLEAF, INFO[0], INFO[1], INFO[2], INFO[3])
		#define GETCPU(CPU) {							  \
			uint32_t CPUInfo[4];						   \
			CPUID(CPUInfo, 1, 0);						  \
			/* CPUInfo[1] is EBX, bits 24-31 are APIC ID */ \
			if ( (CPUInfo[3] & (1 << 9)) == 0) {		   \
			CPU = -1;  /* no APIC on chip */			 \
			}											  \
			else {										 \
			CPU = (unsigned)CPUInfo[1] >> 24;					\
			}											  \
			if (CPU < 0) CPU = 0;						  \
		}
		SAI cid_t mp_coreid(){
			// get_cpu();
			cid_t ret;
			GETCPU(ret);
			return ret;
		}
		#else
		SAI cid_t mp_coreid(){
			return 0;
		}
		#endif
	
	#else
	#include <pthread.h>
	extern int sched_getcpu (void) __THROW;
	SAI cid_t mp_coreid(){
		return sched_getcpu();
	}
	#endif
#endif
